package com.wanwen.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanwen.dto.AskerDto;
import com.wanwen.entity.Asker;
import com.wanwen.mapper.AskerMapper;
import com.wanwen.utils.Result;
import com.wanwen.utils.ResultData;
import com.wanwen.utils.StringUtils;

@RestController
@RequestMapping("/asker")
public class AskerController {
	@Autowired
	private AskerMapper askerMapper;

	/**
	 * /asker/getAll 查询
	 * 
	 * @param Page,remark
	 * @return ResultData
	 */
	@RequestMapping("/getAll")
	public Object dynamicQuery(Page<AskerDto> Page, String remark) {
		QueryWrapper<AskerDto> queryWrapper = new QueryWrapper<AskerDto>();
		queryWrapper.eq(StringUtils.isnotNull(remark), "asker.remark", remark);
		queryWrapper.eq("users.deleted", 0);
		queryWrapper.apply("users.id = asker.uid");
		IPage<AskerDto> signinPage = askerMapper.findAsker(Page, queryWrapper);
		return new ResultData(0, "", signinPage.getRecords(), signinPage.getTotal());
	}

	/**
	 * /asker/update 
	 * @param ids
	 * @return Result
	 */
	@RequestMapping("/updateWeight")
	public Object updateWeight(String idStr) {
		String[] ids = idStr.split(",");
		int i=0;
		for (String id : ids) {
			i++;
			UpdateWrapper<Asker> SigninUpdateWrapper = new UpdateWrapper<Asker>()
					.set("weight", i)
					.in("id", id);
			askerMapper.update(new Asker(Integer.parseInt(id),i), SigninUpdateWrapper);
		}
		return new Result("设置失败", null, i != 0 ? true : false);
	}
}
